﻿

using System;
using System.Data;
using Model;
namespace Dal
{
	public class DbCountryDal
	{
		public DbCountryDal()
		{
		}
 		#region Insert
 		public static int Insert(DbCountry dbCountry)
		{	
			string sql = @"Insert Into DbCountry (
					Country,
					CountryISOCode
				) values('{0}','{1}')";
			sql = string.Format(sql,
					SqlHelper.String(dbCountry.Country),
					SqlHelper.String(dbCountry.CountryISOCode)
				);
			return SqlHelper.ExecSql(sql);
		}
		#endregion
		
		#region Update
 		public static int Update(DbCountry dbCountry)
		{	
			string sql = @"Update DbCountry set 

					Country= '{0}',
					CountryISOCode= '{1}'
				where

					CId= {2}";
			sql = string.Format(sql,
					SqlHelper.String(dbCountry.Country),
					SqlHelper.String(dbCountry.CountryISOCode),
					dbCountry.CId);
			return SqlHelper.ExecSql(sql);
		}
		#endregion
 
 

 		#region Delete
 		public static int Delete(Int32 cId)
		{
		
			string sql = @"Delete from DbCountry where 
					CId= {0}";
			sql = string.Format(sql,
					cId
);
			return SqlHelper.ExecSql(sql);
		} 
		#endregion
 
 		#region Select
 		
 		public static DataSet Select()
		{
			string sql = "select a.* from DbCountry a "; 
			return SqlHelper.Select(sql);
		}
				
		public static DbCountry Select(Int32 cId)
		{
			DbCountry dbCountry = null;
			string sql = @"select a.* from DbCountry a  where
					a.CId= {0}
";
			sql = string.Format(sql,
					cId
);
			DataSet ds = SqlHelper.Select(sql);
			if(SqlHelper.HasRow(ds))
				dbCountry = new DbCountry(ds.Tables[0].Rows[0]);					
			return dbCountry;
		}
		
		public static DataSet Select(string where)
		{ 
			if (where.Trim().ToLower().StartsWith("where"))
				where = where.Substring(5);
			if (!where.Trim().ToLower().StartsWith("and") && where.Trim().Length > 0)
				where = " and " + where;
			string sql = "select a.* from DbCountry a  where 1=1 " + where ;
			return SqlHelper.Select(sql);
		}
		
		public static DataSet SelectForList(string where,int pageIndex,int pageSize,bool bCount,out int count )
		{
			count = 0;
						
			if (where.Trim().ToLower().StartsWith("where"))
				where = where.Substring(5);
			if (!where.Trim().ToLower().StartsWith("and") && where.Trim().Length > 0)
				where = " and " + where;
			string sql = "select a.* from dbCountry a where 1=1 " + where;
                
			return SqlHelper.Select(sql,pageSize,pageIndex,bCount,ref count);
		}		
		public static DataSet Select(string where,int pageIndex,int pageSize,bool bCount,out int count )
		{
			count = 0;			
			if (where.Trim().ToLower().StartsWith("where"))
				where = where.Substring(5);
			if (!where.Trim().ToLower().StartsWith("and") && where.Trim().Length > 0)
				where = " and " + where;			
			string sql = "select a.* from DbCountry a  where 1=1 " + where;
			return SqlHelper.Select(sql,pageSize,pageIndex,bCount,ref count);
		}
		#endregion
	}
}
